//@version=5
indicator(title="PFE (Polarized Fractal Efficiency)", shorttitle="PFE", overlay=false)

// Input settings
useFixedValue = input.bool(false, title="Use Fixed Value?")
assetType = input.string("forex", title="Asset Type", options=["forex", "crypto", "metals", "stocks"])
timeFrame = input.string("1d", title="Time Frame", options=["4h", "1d"])
UseAverage = input.bool(true, title="Use Average?")
PfePeriod = input.int(9, minval=1, title="PFE Period")
MaPeriod = input.int(5, minval=1, title="MA Period")

// Set fixed values if needed
if useFixedValue
    if assetType == "forex" and timeFrame == "1d"
        PfePeriod := 19
        UseAverage := true
        MaPeriod := 5
    else if assetType == "forex" and timeFrame == "4h"
        PfePeriod := 8
        UseAverage := true
        MaPeriod := 9
    else if assetType == "crypto" and timeFrame == "1d"
        PfePeriod := 67
        UseAverage := false
        MaPeriod := 86
    else if assetType == "crypto" and timeFrame == "4h"
        PfePeriod := 50
        UseAverage := true
        MaPeriod := 12
    else if assetType == "metals" and timeFrame == "1d"
        PfePeriod := 4
        UseAverage := true
        MaPeriod := 58
    else if assetType == "metals" and timeFrame == "4h"
        PfePeriod := 55
        UseAverage := true
        MaPeriod := 7
    else if assetType == "stocks" and timeFrame == "1d"
        PfePeriod := 72
        UseAverage := false
        MaPeriod := 64
    else if assetType == "stocks" and timeFrame == "4h"
        PfePeriod := 31
        UseAverage := false
        MaPeriod := 61

// PFE Calculation
PFE = math.sqrt(math.pow(close - close[PfePeriod], 2) + 100)
C2C = math.sum(math.sqrt(math.pow((close - close[1]), 2) + 1), PfePeriod)
xFracEff = close - close[PfePeriod] > 0 ?  math.round((PFE / C2C) * 100) : math.round(-(PFE / C2C) * 100)

// Apply moving average if UseAverage is true
xEMA = UseAverage ? ta.ema(xFracEff, MaPeriod) : xFracEff

// Plot the PFE
plot(xEMA, color=color.yellow, title="PFE")
hline(0, "Zero Line", color=color.blue, linewidth=1)


// 0-line crossover detection
crossOver = ta.crossover(xEMA, 0)
crossUnder = ta.crossunder(xEMA, 0)

// Plot shapes for crossovers
plotshape(crossOver, title="Crossover Up", location=location.bottom, color=color.green, style=shape.triangleup, size=size.tiny)
plotshape(crossUnder, title="Crossover Down", location=location.bottom, color=color.red, style=shape.triangledown, size=size.tiny)
